package com.example.b_308.myapplication.view.user.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RadioGroup;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.comment.utils.Global;
import com.example.b_308.myapplication.comment.view.page.WorkPageAdapter;
import com.example.b_308.myapplication.comment.view.page.WorkViewPager;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.model.Company;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.model.base.BaseModel;
import com.example.b_308.myapplication.view.base.BaseActivity;
import com.example.b_308.myapplication.view.base.BaseFragment;
import com.example.b_308.myapplication.view.main.MainActivity;
import com.example.b_308.myapplication.view.user.login.IUserView;
import com.example.b_308.myapplication.view.user.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by B-308 on 2017/11/20.
 */
public class RegisterActivity extends BaseActivity implements IUserView {
    WorkViewPager workViewPagerRegister;
    RadioGroup radioGroup;
    List<BaseFragment> fragmentsRegister = new ArrayList<>();
    int current = 0;
    UserController userController;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        workViewPagerRegister = (WorkViewPager) findViewById(R.id.work_view_page_register);
        radioGroup = (RadioGroup) findViewById(R.id.rg_bottom_register);
        getTitleBar().setTitle(Global.getString(R.string.register));
        getTitleBar().getTv_setting().setVisibility(View.GONE);
        getTitleBar().getIv_setting().setVisibility(View.GONE);
        getTitleBar().getTv_return().setVisibility(View.GONE);
        fragmentsRegister.add(new VerifyFragment().setTitle("手机验证"));
        fragmentsRegister.add(new VerifyNewPasswordFragment().setTitle("手机验证"));
        fragmentsRegister.add(new ConsummateFragment().setTitle("信息完善"));
        fragmentsRegister.add(new ConsummatePersonFragment().setTitle("信息完善"));
        fragmentsRegister.add(new RegSuccessFragment().setTitle("注册成功"));
        workViewPagerRegister.setAdapter(new WorkPageAdapter(getSupportFragmentManager(), fragmentsRegister));
        workViewPagerRegister.setCurrentItem(current);

    }

    Company company;
    Staff staff;

    public <T> void next(BaseFragment baseFragment, T t) {
        int index = fragmentsRegister.indexOf(baseFragment);
        if (null == company)
            company = new Company();
        if (null == staff)
            staff = new Staff();
        switch (index) {
            case 0:
                //获取验证码--rg与vp的变化在获取验证码成功的时候处理
                if (t instanceof String) {
                    staff.setPhone((String) t);
                    staff.put("code","11");
                    //获取验证码
                    workViewPagerRegister.setCurrentItem(1);
                }
                break;
            case 1:
                //设置密码 当密码格式正确且两次输入相同的时候，设置staff的帕asword属性为参数值，
                // 并且设置rg第二个button选中，vp的第三个元素选中
                if (t instanceof String) {
                    staff.put("password", t.toString());
                    workViewPagerRegister.setCurrentItem(2);
                    radioGroup.getChildAt(1).setSelected(true);
                }
                break;
            case 2:
                //完善公司信息，循环迭代公司对象属性存放的map，完成过后设置vp第四个元素选中，rg
                if (t instanceof Company) {
                    company = (Company) t;
                    workViewPagerRegister.setCurrentItem(3);
                }
                break;
            case 3:
                //将参数中staff的属性保存到全局变量staff中，同时调用userCOntroller注册。vp和rg的变化在注册成功的时候处理
                if (t instanceof Staff) {
                    staff.putAttrs((BaseModel) t);
                    if (null == userController) {
                        userController = new UserController(this);
                        userController.register(company, staff);
                    }
                }
                break;
            case 4:
                //注册成功点击体验回到登录界面并且销毁当前界面
                tend2(LoginActivity.class);
                finish();
                break;
        }
    }


    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {
        //rg第三个元素设置为选中
        radioGroup.getChildAt(2).setSelected(true);
        //vp第五个元素设置为选中
        workViewPagerRegister.setCurrentItem(4);
    }

    @Override
    public void getCodeSuccess() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 2:
                fragmentsRegister.get(2).onActivityResult(requestCode,resultCode,data);
                break;
        }
    }
}
